﻿using Inovout.People.Models;
using Inovout.Security;
using System;
using System.ComponentModel.DataAnnotations;

namespace Inovout.Account.Web.Models
{
    public class PersonSignUpInfo : SignUpInfo
    {
        public int? MembershipId { get; set; }

        [Required(ErrorMessage = "用户名不可为空")]
        [EmailAddress(ErrorMessage = "请输入正确的邮箱格式")]
        [RegularExpression("^((?!@samples.cn).)*$", ErrorMessage = "该邮箱已注册")]
        [Alias("用户名")]
        public override string UserName
        {
            get
            {
                return base.UserName;
            }
            set
            {
                base.UserName = value;
            }
        }
        [Required(ErrorMessage = "姓名不可为空")]
        [Alias("真实姓名")]
        public string PersonName { get; set; }

        private string personEmail;
        [Required(ErrorMessage = "邮箱地址不可为空")]
        [EmailAddress(ErrorMessage = "请输入正确的邮箱格式")]
        [RegularExpression("^((?!@samples.cn).)*$", ErrorMessage = "该邮箱已注册")]
        [Alias("邮箱地址")]
        public  string PersonEmail
        {
            get
            {
                if (string.IsNullOrEmpty(personEmail))
                {
                    return UserName;
                }
                return personEmail;
            }
            set
            {
                personEmail = value;
            }
        }

        [Required(ErrorMessage = "手机号码不可为空")]
        [RegularExpression("^1[358][0-9]{9}$", ErrorMessage = "请输入11位有效手机号码")]
        [Alias("手机号码")]
        public string PersonMobile { get; set; }

        public Person Person
        {
            get
            {
                return new Person { Name = PersonName, Email = PersonEmail, Mobile = PersonMobile };
            }
        }

          public FormsUserCredential FormsUserCredential
        {
            get
            {
                SystemUser systemUser = new SystemUser();
                systemUser.Name = Guid.NewGuid().ToString();
                return new FormsUserCredential
                {
                    Email = PersonEmail,
                    AccountName = PersonName,
                    Provider = "Inovout",
                    Identifier = Guid.NewGuid().ToString(),
                    UserName = UserName,
                    Password = Password,
                    SystemUser = systemUser
                };
            }
        }

        public OpenIdUserCredential OpenIdUserCredential
        {
            get
            {
                SystemUser systemUser = new SystemUser();
                systemUser.Name = OpenIdUserName;
                return new OpenIdUserCredential
                {
                    Email = PersonEmail,
                    AccountName = PersonName,
                    Provider = OpenIdProvider,
                    Identifier = OpenIdClaimedIdentifier,
                    SystemUser = systemUser
                };
            }
        }

        public OAuthUserCredential OAuthUserCredential
        {
            get
            {
                SystemUser systemUser = new SystemUser();
                systemUser.Name = OpenIdUserName;
                return new OAuthUserCredential
                {
                    Email = PersonEmail,
                    AccountName = PersonName,
                    Provider = OpenIdProvider,
                    Identifier = OpenIdClaimedIdentifier,
                    Code =Code,
                    AccessToken = AccessToken,
                    AccessTokenExpiredTime = AccessTokenExpiredTime,
                    RefreshToken = RefreshToken,
                    RefreshTokenExpiredTime = RefreshTokenExpiredTime,
                    SystemUser = systemUser
                };
            }
        }
    } 
}